import request from '@/utils/request'
export const setToken = (data) => ({ type: 'login/token', payload: data })

/**
 * 发送验证码
 * @param mobile 手机号
 * @returns {(function(*): Promise<void>)|*}
 */
export const sendCode = (mobile) => {
  return async (dispatch) => {
    await request('/sms/codes/' + mobile)
  }
}

/**
 * 登录
 * @param data 手机号/验证码
 */
export const login = (data) => {
  return async (dispatch) => {
    const res = await request.post('/authorizations', data)
    // 返回的token和refresh_token
    await dispatch(setToken(res))
  }
}
